<?php

namespace app\apigovernment\controller;

use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 招聘会地区表
 */
class InviteAddress extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\apigovernment\model\InviteAddress();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $where = [];
            if (!empty($input['id'])) {
                $where['id'] = $input['id'];
            }
            if (!empty($input['keyWork'])) {
                $where['name'] = ['like', '%' . $input['keyWork'] . '%'];
            }
            if (!empty($input['status'])) {
                $where['status'] = $input['status'];
            }
            $order = 'sort desc';
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->alias('m')
                ->where($where)
                ->order($order)
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);

            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 保存
     * @return void
     */
    public function save()
    {
        $input = input('post.');
        try {
            $id = $input['id'];
            unset($input['id']);
            $input['createtime'] = date('Y-m-d H:i:s');
            if (empty($id)) {
                $this->model->insert($input);
            } else {
                $this->model->where('id', $id)->update($input);
            }
            $this->success('ok');
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 删除
     * @return void
     */
    public function del()
    {
        $input = input('post.');
        try {
            $this->model->where('id', $input['id'])->delete();
            $this->success('ok');
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

}